package cn.hs.dao.impl;

import cn.hs.dao.CourierstationDao;
import cn.hs.entity.Courierstation;
import cn.hs.entity.Shop;
import cn.hs.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierstationDaoImpl implements CourierstationDao {
    @Override
    public int insertCourierstation(Courierstation courierstation) {
        return 0;
    }

    @Override
    public int updateCourierstationp(Courierstation courierstation) {
        return 0;
    }

    @Override
    public List<Courierstation> selectCourierstations(Map<String, String> map) {
        List<Courierstation> list = new ArrayList<>();
        //动态sql
        StringBuffer sb = new StringBuffer("select * from courierstation where 1=1 ");
        //动态参数
        List param = new ArrayList();
        String courierStationName = map.get("courierStationName");
        if (courierStationName!=null && !"".equals(courierStationName)){
            sb.append(" and courierStationName like ? ");
            param.add("%"+courierStationName+"%");
        }
//        //
        String communityName = map.get("communityName");
        if (communityName!=null && !"".equals(communityName)){
            sb.append(" and courierStationCommunityName like ? ");
            param.add("%"+communityName+"%");
        }
        //判断驿站状态
        String courierStationStatus = map.get("courierStationStatus");
        if (courierStationStatus!=null && !"".equals(courierStationStatus)){
            sb.append(" and courierStationStatus = ? ");
            param.add(courierStationStatus);
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" limit ?,? ");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);
        String sql = sb.toString();
        Object[] objects = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, objects);
        try {
            while (resultSet.next()){
                Courierstation courierstation = new Courierstation(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getInt(8),
                        resultSet.getDate(9),
                        resultSet.getInt(10),
                        resultSet.getInt(11),
                        resultSet.getString(12),
                        resultSet.getInt(13)
                );
                list.add(courierstation);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectCourierstationCount(Map<String, String> map) {
        int total = 0;
        //动态sql
        StringBuffer sb = new StringBuffer("select count(*) from courierstation where 1=1 ");
        //动态参数
        List param = new ArrayList();
        String courierStationName = map.get("courierStationName");
        if (courierStationName!=null && !"".equals(courierStationName)){
            sb.append(" and courierStationName like ? ");
            param.add("%"+courierStationName+"%");
        }
        //判断小区名称是否存在
        String communityName = map.get("communityName");
        if (communityName!=null && !"".equals(communityName)){
            sb.append(" and courierStationCommunityName like ? ");
            param.add("%"+communityName+"%");
        }
        //判断驿站状态
        String courierStationStatus = map.get("courierStationStatus");
        if (courierStationStatus!=null && !"".equals(courierStationStatus)){
            sb.append(" and courierStationStatus = ? ");
            param.add(courierStationStatus);
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }

        String sql = sb.toString();
        System.out.println(sql);
        Object[] objects = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, objects);
        try {
            if (resultSet.next()){
                 total = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return total;
    }
}
